﻿app.controller('HomeController', function($scope, LoginFactory, TodoFactory){
    // Logged in status
    $scope.status = LoginFactory.status;

    //Current todos
    $scope.cTodos = {};

    $scope.Greeting = "Hello all";
    // User Credentials
    $scope.user = {
        username: '',
        password: ''
    }
    // user info
    $scope.userInfo = {};

    // User clicked Login..
    $scope.login = function () {
        LoginFactory.login($scope.user).then(function (data) {
            console.log("login: ", data);
            $scope.getUserInfo();
        });
    }

    // Get User info 
    $scope.getUserInfo = function () {
        LoginFactory.getInfo().then(function (data) {
            console.log(data);
            $scope.userInfo = data;
            //$scope.user.username = data.userName;
            if (data == "Admin") {
                // make API call for all TODOS
                TodoFactory.getAll().then(function (data) {
                    $scope.cTodos = data;
                    console.log(data);
                })
            }
            if (data == "User") {
                // make api for specifc (by ID) TODOS
                TodoFactory.getById().then(function (data) {
                    $scope.cTodos = data;
                    console.log(data);
                })
            }

        })
    }

    // Checks to see if user is already logged in
    // And if so, gets user info

    // if ($scope.status.loggedIn) {
    if ($scope.status.loggedin) {
        console.log(LoginFactory.status);
        alert('try!!!!');
        $scope.getUserInfo();
    }

    // Allow user to log out
    $scope.logout = function () {
        //alert('out');
        LoginFactory.logout();
    }

})